/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.ui;

import com.ivanbiz.dao.DaftarKelasDAO;
import com.ivanbiz.dao.InvoiceDAO;
import com.ivanbiz.dao.impl.DaftarKelasDAOImpl;
import com.ivanbiz.dao.impl.InvoiceDAOImpl;
import com.ivanbiz.model.Bank;
import com.ivanbiz.model.DaftarKelas;
import com.ivanbiz.model.Invoice;
import com.ivanbiz.model.Kelas;
import com.ivanbiz.report.TagihanReport;
import com.ivanbiz.service.RenderingKanan;
import com.ivanbiz.service.ServiceHelper;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

/**
 *
 * @author Shbt Peterpan
 */
public class TagihanReportDialog extends javax.swing.JDialog {

    Invoice invoice;
    InvoiceDAO invoiceDAO;
    List<Invoice> listInvoice;
    SimpleDateFormat sdf;
    NumberFormat numberFormat;
    List<DaftarKelas> listDaftarKelas;
    DaftarKelasDAO daftarKelasDAO;

    public TagihanReportDialog() {
        initComponents();
        invoiceDAO = new InvoiceDAOImpl();
        daftarKelasDAO = new DaftarKelasDAOImpl();
        sdf = new SimpleDateFormat("dd-MMMM-yyyy");
        numberFormat = NumberFormat.getNumberInstance();
        refresh();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        buttonCetak = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        comboBoxPencarian = new javax.swing.JComboBox();
        textCari = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        tableTagihan = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setAlwaysOnTop(true);
        setModal(true);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24));
        jLabel1.setText("Laporan Tagihan");

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/preview.jpg"))); // NOI18N
        jButton1.setText("Lihat Tagihan Terseleksi");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1);

        buttonCetak.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/cetak.jpg"))); // NOI18N
        buttonCetak.setText("Cetak Tagihan Terseleksi");
        buttonCetak.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCetakActionPerformed(evt);
            }
        });
        jPanel1.add(buttonCetak);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(""), "Pencarian"));

        textCari.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                textCariKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(comboBoxPencarian, javax.swing.GroupLayout.PREFERRED_SIZE, 235, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textCari, javax.swing.GroupLayout.DEFAULT_SIZE, 499, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(comboBoxPencarian, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textCari, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(8, Short.MAX_VALUE))
        );

        tableTagihan.setAutoCreateRowSorter(true);
        tableTagihan.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(tableTagihan);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 780, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 780, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 780, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 426, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-816)/2, (screenSize.height-638)/2, 816, 638);
    }// </editor-fold>//GEN-END:initComponents

    private void buttonCetakActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCetakActionPerformed
        if (tableTagihan.getSelectedRow() == -1) {
            JOptionPane.showMessageDialog(this, "Pilih data yang akan dicetak", "warning", JOptionPane.WARNING_MESSAGE);
        } else {
            try {
                Kelas kelas = listInvoice.get(tableTagihan.getSelectedRow()).getKelas();
                Bank bank = listInvoice.get(tableTagihan.getSelectedRow()).getBank();
                listDaftarKelas = daftarKelasDAO.getDataByEqualsMore(kelas, bank);
                new TagihanReport().previewAndCetakTagihan(listInvoice.get(tableTagihan.getSelectedRow()), listDaftarKelas, "preview");
                refresh();
            } catch (Exception ex) {
                Logger.getLogger(TagihanReportDialog.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_buttonCetakActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (tableTagihan.getSelectedRow() == -1) {
            JOptionPane.showMessageDialog(this, "Pilih data yang akan dilihat", "warning", JOptionPane.WARNING_MESSAGE);
        } else {
            try {
                Kelas kelas = listInvoice.get(tableTagihan.getSelectedRow()).getKelas();
                Bank bank = listInvoice.get(tableTagihan.getSelectedRow()).getBank();
                listDaftarKelas = daftarKelasDAO.getDataByEqualsMore(kelas, bank);
                if (!listDaftarKelas.isEmpty()) {
                    new TagihanReport().previewAndCetakTagihan(listInvoice.get(tableTagihan.getSelectedRow()), listDaftarKelas, "preview");
                }
                refresh();
            } catch (Exception ex) {
                Logger.getLogger(TagihanReportDialog.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void textCariKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textCariKeyReleased
        try {
            listInvoice = invoiceDAO.getDataByLike(Invoice.class, comboBoxPencarian.getSelectedItem().toString(), textCari.getText());
            updateTableTagihan();
        } catch (Exception ex) {
            Logger.getLogger(TagihanReportDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_textCariKeyReleased
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonCetak;
    private javax.swing.JComboBox comboBoxPencarian;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tableTagihan;
    private javax.swing.JTextField textCari;
    // End of variables declaration//GEN-END:variables

    private void refresh() {
        try {
            listInvoice = invoiceDAO.getDataByNotEquals(Invoice.class, "status", "0");
            updateTableTagihan();
            updateComboPencarian();
        } catch (Exception ex) {
            Logger.getLogger(TagihanReportDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void updateTableTagihan() {
        String[] judul = {"No", "Date", "NII", "Kelas", "Bank", "Kepada", "Untuk Pembayaran", "Jumlah Peserta", "Jumlah Tagihan", "Jatuh Tempo", "Mohon Ditransfer Ke", "Proses"};
        Object[][] isi = new Object[listInvoice.size()][12];
        int x = 0;
        int no = 0;
        for (Invoice invoices : listInvoice) {
            no += 1;
            String[] kepada;
            kepada = invoices.getDeskripsiKepada().split("#");
            String[] deskripsi;
            deskripsi = invoices.getDeskripsiUntuk().split("#");
            isi[x][0] = no;
            isi[x][1] = sdf.format(invoices.getDate());
            isi[x][2] = invoices.getNII();
            isi[x][3] = invoices.getKelas().getTransactionReference();
            isi[x][4] = invoices.getBank().getNama();
            isi[x][5] = kepada[0];
            isi[x][6] = invoices.getDeskripsiUntukPembayaran();
            isi[x][7] = invoices.getDeskripsiJumlahPeserta();
            isi[x][8] = numberFormat.format(invoices.getJumlahTagihan());
            isi[x][9] = invoices.getJatuhTempo();
            isi[x][10] = deskripsi[1] + " A/C No. " + deskripsi[0];
            isi[x][11] = invoices.getStatus().equals("1") ? "N" : "Y";
            x++;
        }
        tableTagihan.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        new ServiceHelper().setAutoRize(isi, judul, tableTagihan);
        tableTagihan.getColumnModel().getColumn(8).setCellRenderer(new RenderingKanan());
    }

    private void updateComboPencarian() {
        Object[] dataClass = new ServiceHelper().getPropertyClass(Invoice.class);
        Object[] dataSearch = new Object[1];
        dataSearch[0] = dataClass[1];
        comboBoxPencarian.setModel(new DefaultComboBoxModel(dataSearch));
    }
}
